/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.BugRS.dao;

import com.aptech.BugRS.enity.DepartmentDTO;
import com.aptech.BugRS.util.Configure;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author KISS
 */
public class DepartmentDAO {

    private Configure db = null;
    private Connection con = null;
    private ResultSet rs = null;
    private PreparedStatement pst = null;
    private String lastError = "";

    /**
     * Creates a new instance of D_Department
     */
    public DepartmentDAO() {
        db = new Configure();
    }

    /**
     *
     * @return list of Complaint
     */
    public ArrayList<DepartmentDTO> ReadByAll() {

        ArrayList<DepartmentDTO> list = new ArrayList<DepartmentDTO>();
        con = db.getConnection();
        String sql = "SELECT * FROM Department";
        try {
            pst = con.prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()) {
                DepartmentDTO dep = new DepartmentDTO();
                dep.setDepartmentID(rs.getInt(1));
                dep.setDepartmentName(rs.getString(2));
                list.add(dep);
            }
        } catch (SQLException ex) {
            setLastError("SQL Error");
        } finally {
            db.closeConnection();
        }
        return list;
    }

    /**
     * Read by ID of Department
     * @param id
     * @return department
     */
    public DepartmentDTO ReadByID(int id) {
        DepartmentDTO dep = new DepartmentDTO();
        try {
            con = db.getConnection();
            String sql = "SELECT * FROM Department WHERE DepartmentID = ?";
            pst = con.prepareStatement(sql);
            pst.setInt(1, id);
            rs = pst.executeQuery();
            dep.setDepartmentID(rs.getInt(1));
            dep.setDepartmentName(rs.getString(2));
        } catch (SQLException ex) {
            setLastError("SQL Error");
        }
        return dep;
    }

    /**
     * Method add new department
     * @param dep
     * @return boolean
     */
    public boolean Create(DepartmentDTO dep) {

        ArrayList<DepartmentDTO> list = ReadByAll();
        /*
         * Check exits in database
         */
        for (DepartmentDTO de : list) {
            if (de.getDepartmentID() == de.getDepartmentID()) {
                setLastError("Department is exitsed! Add fail!");
                return false;
            }
        }
        try {
            con = db.getConnection();
            String sql = "INSERT INTO Department VALUES(?,?,?,?,?,?,?)";
            pst = con.prepareStatement(sql);
            pst.setInt(1, dep.getDepartmentID());
            pst.setString(2, dep.getDepartmentName());
            if (pst.executeUpdate() > 0) {
                setLastError("Add Department Successfully!");
                db.closeConnection();
                return true;
            }
        } catch (SQLException ex) {
            setLastError("SQL Error!");
            db.closeConnection();
            return false;
        }
        setLastError("Add fail!");
        db.closeConnection();
        return false;
    }

    /**
     * check Exits of Department
     * @param dep
     * @return boolean
     */
    public boolean isExits(DepartmentDTO dep) {
        try {
            String sql = "SELECT * FROM Department WHERE DepartmentID = ?";
            con = db.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1, dep.getDepartmentID());
            rs = pst.executeQuery();
            /**
             * Exits return true
             * Not Exit return false;
             */
            if (rs.next()) {
                return true;
            }

        } catch (SQLException ex) {
            setLastError("SQL Error");
        }
        return false;
    }

    /**
     * Method Update a Department
     * @param dep
     * @return boolean
     */
    public boolean Update(DepartmentDTO dep) {
        try {
            con = db.getConnection();
            String sql = "SELECT * FROM Complaint WHERE ComplaintID = ?";
            pst = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            pst.setInt(1, dep.getDepartmentID());
            rs = pst.executeQuery();
            rs.first();
            rs.updateString(2, dep.getDepartmentName());
            rs.updateRow();
            db.closeConnection();
            setLastError("Update Department successfully!");
            return true;
        } catch (SQLException ex) {
            setLastError("SQL Error");
            db.closeConnection();
            return false;
        }
    }

    /**
     * @return the lastError
     */
    public String getLastError() {
        return lastError;
    }

    /**
     * @param lastError the lastError to set
     */
    public void setLastError(String lastError) {
        this.lastError = lastError;
    }
}
